<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" th:href="@{/adminlte/bower_components/bootstrap/dist/css/bootstrap.min.css}">
    <!-- Font Awesome -->
    <link rel="stylesheet" th:href="@{/adminlte/bower_components/font-awesome/css/font-awesome.min.css}">
    <!-- Ionicons -->
    <link rel="stylesheet" th:href="@{/adminlte/bower_components/Ionicons/css/ionicons.min.css}">
    <!-- Theme style -->
    <link rel="stylesheet" th:href="@{/adminlte/dist/css/AdminLTE.min.css}">
    <!-- AdminLTE Skins. We have chosen the skin-blue for this starter
          page. However, you can choose any other skin. Make sure you
          apply the skin class to the body tag so the changes take effect. -->
    <link rel="stylesheet" th:href="@{/adminlte/dist/css/skins/_all-skins.min.css}">

    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>

    <title>标椎地名地址管理系统</title>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

    <div th:replace="common/header :: header"></div>

    <div th:replace="common/menu :: menu"></div>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper" >
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                Page Header
                <small>Optional description</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i>门牌号码管理</a></li>
                <li class="active">录入门牌数据</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content container-fluid">

            <div class="row">
                <!-- left column -->
                <div class="col-md-1">


                </div>
                <!--/.col (left) -->
                <!-- right column -->
                <div class="col-md-10">
                    <!-- Horizontal Form -->
                    <div class="box box-info">
                        <div class="box-header with-border">
                            <h3 class="box-title">Horizontal Form</h3>
                        </div>
                        <!-- /.box-header -->
                        <!-- form start -->
                        <form class="form-horizontal" th:action="@{/Door/add}" method="post" th:object="${door}">
                            <div class="box-body">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">areaName:</label>
                                    <div class="col-sm-10">
                                        <select class="form-control" id="categoryLevel1">

                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">streetName:</label>
                                    <div class="col-sm-10">
                                        <select class="form-control" name="streetId" id="categoryLevel2">

                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="inputDoorNumber" class="col-sm-2 control-label">doorNumber:</label>

                                    <div class="col-sm-10">
                                        <input type="hidden" th:field="*{doorId}" th:value="${door.doorId}" name="doorId">
                                        <input type="text" class="form-control" id="inputDoorNumber" placeholder="请输入" name="doorNumber" th:field="*{doorNumber}">
                                    </div>
                                </div>

<!--                                <div class="form-group">-->
<!--                                    <label class="col-sm-2 control-label">streetType:</label>-->
<!--                                    <div class="col-sm-10">-->
<!--                                        <select class="form-control" name="streetType" id="categoryLevel3">-->
<!--                                            <option value ="">政府部门</option>-->
<!--                                            <option value ="">学校</option>-->
<!--                                            <option value ="">医院</option>-->
<!--                                            <option value ="">商户</option>-->
<!--                                            <option value ="">用户</option>-->
<!--                                        </select>-->
<!--                                    </div>-->
<!--                                </div>-->
                            </div>
                            <!-- /.box-body -->
                            <div class="box-footer">
                                <button type="submit" class="btn btn-default">Cancel</button>
                                <button type="submit" class="btn btn-info pull-right">Submit</button>
                            </div>
                            <!-- /.box-footer -->
                        </form>
                    </div>
                </div>
                <!--/.col (right) -->
                <div class="col-md-1">


                </div>
            </div>

            <!--------------------------
              | Your Page Content Here |
              -------------------------->

            <!--<form th:action="@{/Area/add}" method="post" th:object="${area}">-->
            <!--<table>-->
            <!--<tr>-->
            <!--<td>areaName:</td>-->
            <!--<td>-->
            <!--<input type="hidden" name="areaId" th:field="*{areaId}">-->
            <!--<input type="text" name="areaName" th:field="*{areaName}">-->
            <!--</td>-->
            <!--</tr>-->
            <!--<tr>-->
            <!--<td>areaMap:</td>-->
            <!--<td><input type="text" name="areaMap" th:field="*{areaMap}"></td>-->
            <!--</tr>-->
            <!--<tr>-->
            <!--<td></td>-->
            <!--<td><input type="submit"></td>-->
            <!--</tr>-->
            <!--</table>-->
            <!--</form>-->

        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->

    <div th:replace="common/footer :: footer"></div>



</div>
<!-- ./wrapper -->

<!-- REQUIRED JS SCRIPTS -->

<!-- jQuery 3 -->
<script th:src="@{/adminlte/bower_components/jquery/dist/jquery.min.js}"></script>
<!-- Bootstrap 3.3.7 -->
<script th:src="@{/adminlte/bower_components/bootstrap/dist/js/bootstrap.min.js}"></script>
<!-- AdminLTE App -->
<script th:src="@{/adminlte/dist/js/adminlte.min.js}"></script>

<script>
    $(document).ready(function(){

        var streetId = '[[${door.streetId}]]'
        // alert(streetId);
        if(streetId != null){
            $.get('/Door/findStreetByDoor?id=' + streetId,function(street){
                // alert(street.streetId);
                $.get('/Door/findAreaByStreet?id=' + street.areaId,function(area){
                    // alert(area.areaId);

                    var categoryLevel1 = $("#categoryLevel1"); //拿到一级下拉框元素
                    categoryLevel1.append("<option>请选择</option>")
                    $.ajax({
                        type: "GET",//请求类型
                        url: "/Door/findArea",//请求的url
                        dataType: "json",//ajax接口（请求url）返回的数据类型
                        success: function (data) {
                            if (data != null) {
                                for (var i = 0; i < data.length; i++) { //将值填充到下拉框
                                    if(data[i].areaId == area.areaId){
                                        categoryLevel1.append('<option value="' + data[i].areaId + '" selected>' + data[i].areaName + '</option>')
                                    }else {
                                        categoryLevel1.append('<option value="' + data[i].areaId + '">' + data[i].areaName + '</option>')
                                    }
                                }
                            }
                        },
                        error: function (data) {
                            console.log(data);
                        }
                    });

                    $("#categoryLevel2").find("option").remove(); //把二级下拉框原来的值先清除
                    var categoryLevel2 = $("#categoryLevel2");//拿到二级下拉框元素
                    if (area.areaId != null) {
                        $.ajax({
                            type: "GET",//请求类型
                            url: "/Door/findStreetByArea",//请求的url
                            data: {id: area.areaId},//请求参数
                            dataType: "json",//ajax接口（请求url）返回的数据类型
                            success: function (data) {//data：返回数据（json对象）
                                if (data != null) {
                                    for (var i = 0; i < data.length; i++) {//将值填充到下拉框
                                        if(data[i].streetId == street.streetId){
                                            $(categoryLevel2).append('<option value="' + data[i].streetId + '" selected>' + data[i].streetName + '</option>')
                                        }else {
                                            $(categoryLevel2).append('<option value="' + data[i].streetId + '">' + data[i].streetName + '</option>')
                                        }
                                    }
                                }
                            },
                            error: function (data) {
                                console.log(data);
                            }
                        })
                    }

                    $("#categoryLevel1").change(function () { //当一级下拉框改变时
                        $("#categoryLevel2").find("option").remove(); //把二级下拉框原来的值先清除
                        var areaId = $("#categoryLevel1").val();//拿到一级下拉框值
                        var categoryLevel2 = $("#categoryLevel2");//拿到二级下拉框元素
                        categoryLevel2.append("<option>请选择</option>")
                        if (areaId != null) {
                            $.ajax({
                                type: "GET",//请求类型
                                url: "/Door/findStreetByArea",//请求的url
                                data: {id: areaId},//请求参数
                                dataType: "json",//ajax接口（请求url）返回的数据类型
                                success: function (data) {//data：返回数据（json对象）
                                    if (data != null) {
                                        for (var i = 0; i < data.length; i++) {//将值填充到下拉框
                                                $(categoryLevel2).append('<option value="' + data[i].streetId + '">' + data[i].streetName + '</option>')
                                        }
                                    }
                                },
                                error: function (data) {
                                    console.log(data);
                                }
                            })
                        }
                    });

                });
            });
        }else {
            var categoryLevel1 = $("#categoryLevel1"); //拿到一级下拉框元素
            categoryLevel1.append("<option>请选择</option>")
            $.ajax({
                type: "GET",//请求类型
                url: "/User/findArea",//请求的url
                dataType: "json",//ajax接口（请求url）返回的数据类型
                success: function (data) {
                    if (data != null) {
                        for (var i = 0; i < data.length; i++) { //将值填充到下拉框
                            categoryLevel1.append('<option value="' + data[i].areaId + '">' + data[i].areaName + '</option>')
                        }
                    }
                },
                error: function (data) {
                    console.log(data);
                }
            });
            $("#categoryLevel1").change(function () { //当一级下拉框改变时
                $("#categoryLevel2").find("option").remove(); //把二级下拉框原来的值先清除
                var areaId = $("#categoryLevel1").val();//拿到一级下拉框值
                var categoryLevel2 = $("#categoryLevel2");//拿到二级下拉框元素
                categoryLevel2.append("<option>请选择</option>")
                if (areaId != null) {
                    $.ajax({
                        type: "GET",//请求类型
                        url: "/Door/findStreetByArea",//请求的url
                        data: {id: areaId},//请求参数
                        dataType: "json",//ajax接口（请求url）返回的数据类型
                        success: function (data) {//data：返回数据（json对象）
                            if (data != null) {
                                for (var i = 0; i < data.length; i++) {//将值填充到下拉框
                                        $(categoryLevel2).append('<option value="' + data[i].streetId + '">' + data[i].streetName + '</option>')
                                }
                            }
                        },
                        error: function (data) {
                            console.log(data);
                        }
                    })
                }
            });
        }
    });


</script>

<!-- Optionally, you can add Slimscroll and FastClick plugins.
     Both of these plugins are recommended to enhance the
     user experience. -->
</body>
</html>